package com.task;

import java.net.URL;

import javax.xml.namespace.QName;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.service.IWsService;
import com.service.WsServiceImpl;
import com.util.AppUtils;
import com.wsclient.App_002fSgpmssInfCenterService;
import com.wsclient.App_002fSgpmssInfCenterServicePortType;

/**
 * 
 * @ClassName:callWsTask 定时调用接口服务
 * @Description: TODO
 * @author SLH
 * @date 2017-10-06 11:43:34
 * 
 */

@Service("callWsTask")
public class CallWsTask {
	
	 private static  QName SERVICE_NAME = null;
	 private static  URL wsdlURL = null;
	 private static  App_002fSgpmssInfCenterService ss = null;
	 private static  App_002fSgpmssInfCenterServicePortType port = null;
	 
	 static{
//		  SERVICE_NAME = new QName("http://webservice.interfaceCenter.appws.sgpmss.com", "app/sgpmssInfCenterService");
//		  wsdlURL = App_002fSgpmssInfCenterService.WSDL_LOCATION;
//		  ss = new App_002fSgpmssInfCenterService(wsdlURL, SERVICE_NAME);
//		  port = ss.getApp_002fSgpmssInfCenterServiceHttpPort(); 
	 }
	
	@Autowired
	private IWsService WsService;
	
	@Autowired
	private SystemService systemService;
	
	/*@Scheduled(cron="0 0/1 * * * ?")*/
	public void runDataWork() {
		long start = System.currentTimeMillis();
		org.jeecgframework.core.util.LogUtil.info("===================日工作接口调用定时任务开始===================");
		
		//运行电费回收信息接口
		runPowerRateRecovery();
		
		//运行用检员日常工作接口
		runDataKeyWork();		
		
		org.jeecgframework.core.util.LogUtil.info("===================日工作接口调用定时任务结束===================");
		long end = System.currentTimeMillis();
		long times = end - start;
		org.jeecgframework.core.util.LogUtil.info("总耗时"+times+"毫秒");
	}
	
	public void runMonthWork() {
		long start = System.currentTimeMillis();
		org.jeecgframework.core.util.LogUtil.info("===================月工作接口调用定时任务开始===================");
	
		
		//运行用检员月工作接口
		runMonthKeyWork();		
		
		org.jeecgframework.core.util.LogUtil.info("===================月工作接口调用定时任务结束===================");
		long end = System.currentTimeMillis();
		long times = end - start;
		org.jeecgframework.core.util.LogUtil.info("总耗时"+times+"毫秒");
	}
	
	/**
	 * 获取接口返回总页数
	 * @param interfacename
	 * @param ctime
	 * @return
	 */
	private int getTotalPage(String interfacename,String ctime){
		int totalPage = 0;
		try {
			String paramIn = "<ORDER>"+
								 "<serviceCode>DATA_ZYS</serviceCode>"+
								 "<qtime>"+ctime+"</qtime>"+
								 "<interfacename>"+interfacename+"</interfacename>"+
							  "</ORDER>";

		   System.out.println("DATA_ZYS输入参数=" + paramIn);
	       java.lang.String paramOut = port.mainEntrance(paramIn);
	       System.out.println("返回DATA_ZYS=" + paramOut);

	       if(StringUtils.isNotBlank(paramOut)){
	            Document document = DocumentHelper.parseText(paramOut);  
				Element root = document.getRootElement(); 
				Element flagElement = root.element("flag");
				String flag = flagElement.getText();
				if(StringUtils.isNotBlank(flag) && "1".equals(flag)){
					 totalPage = Integer.parseInt(root.element("jsonData").element("totalpage").getText());
				}
	       }
		} catch (Exception e) {
			e.printStackTrace();
			return totalPage;
		}
		return totalPage;
	}
	
	/**
	 * 定时获取电费回收信息接口
	 */
	private void runPowerRateRecovery(){
		String c_date = AppUtils.getPrevDate();
		int totalPage = getTotalPage("PowerRateRecovery",c_date);
		for(int i=1;i<=totalPage;i++){
			runPowerRateRecoveryByPage(i,c_date);
		}
		
	}
	private void runPowerRateRecoveryByPage(int pageNo,String c_date){
		try {
			String paramIn = "<ORDER>"+
								 "<serviceCode>PowerRateRecovery</serviceCode>"+
								 "<cdate>"+c_date+"</cdate>"+
								 "<pageNo>"+pageNo+"</pageNo>"+
							  "</ORDER>";
			 
			 System.out.println("PowerRateRecovery输入参数=" + paramIn);
		     java.lang.String paramOut = port.mainEntrance(paramIn);
		     System.out.println("返回PowerRateRecovery=" + paramOut);
			
		   if(StringUtils.isNotBlank(paramOut)){
		        Document document = DocumentHelper.parseText(paramOut);  
		        Element root = document.getRootElement(); 
		        Element flagElement = root.element("flag"); 
		        String flag = flagElement.getText();
		        if(StringUtils.isNotBlank(flag) && "1".equals(flag)){
		        	String jsonData = root.element("jsonData").getText();
		        	jsonData = "["+jsonData+"]";
		        	System.out.println("...调用营销业务应用系统电费回收情况接口...///"+jsonData);
		        	WsService.saveTeamPowerRateRecovery(jsonData);
		        }else{
		        	String message = "调用接口PowerRateRecovery失败:"+root.element("errMsg").getText();
		        	systemService.addLog(message, Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR);
		        }
		   }
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 定时获取用检员日常工作接口
	 */
	private void runDataKeyWork(){
		String c_date = AppUtils.getPrevDate();
		int totalPage = getTotalPage("DataKeyWork",c_date);
		for(int i=1;i<=totalPage;i++){
			runDataKeyWorkByPage(i,c_date);
		}
	}
	
	private void runDataKeyWorkByPage(int pageNo,String c_date){
		try {
			String paramIn = "<ORDER>"+
								 "<serviceCode>DataKeyWork</serviceCode>"+
								 "<pageNo>"+pageNo+"</pageNo>"+
								 "<cdate>"+c_date+"</cdate>"+
							  "</ORDER>";
			
			 System.out.println("DataKeyWork输入参数=" + paramIn);
			 java.lang.String paramOut = port.mainEntrance(paramIn);
		     System.out.println("返回DataKeyWork=" + paramOut);

		     if(StringUtils.isNotBlank(paramOut)){
				Document document = DocumentHelper.parseText(paramOut);  
				Element root = document.getRootElement();   
				Element flagElement = root.element("flag"); 
				String flag = flagElement.getText();
				if(StringUtils.isNotBlank(flag) && "1".equals(flag)){				
					String jsonData = root.element("jsonData").getText();
					jsonData = "["+jsonData+"]";
					System.out.println("...调用营销业务应用系统班组巡检员日工作情况接口....///"+jsonData);
					WsService.saveCheckerDdata(jsonData);
				}else{
		        	String message = "调用接口DataKeyWork失败:"+root.element("errMsg").getText();
		        	systemService.addLog(message, Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR);
		        }
		     }
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 定时获取用检员月工作接口
	 */
	private void runMonthKeyWork(){
		String c_month = AppUtils.getCurrMonth();
		int totalPage = getTotalPage("MonthKeyWork",c_month);
		for(int i=1;i<=totalPage;i++){
			runMonthKeyWorkByPage(i,c_month);
		}
	}
	
	private void runMonthKeyWorkByPage(int pageNo,String c_month){
		try {
			String paramIn = "<ORDER>"+
								 "<serviceCode>MonthKeyWork</serviceCode>"+
								 "<pageNo>"+pageNo+"</pageNo>"+
								 "<cmonth>"+c_month+"</cmonth>"+
							  "</ORDER>";
			
			System.out.println("MonthKeyWork输入参数=" + paramIn);
			java.lang.String paramOut = port.mainEntrance(paramIn);
		    System.out.println("返回MonthKeyWork=" + paramOut);

		     if(StringUtils.isNotBlank(paramOut)){
				Document document = DocumentHelper.parseText(paramOut);  
				Element root = document.getRootElement();   
				Element flagElement = root.element("flag"); 
				String flag = flagElement.getText();
				if(StringUtils.isNotBlank(flag) && "1".equals(flag)){				
					String jsonData = root.element("jsonData").getText();
					jsonData = "["+jsonData+"]";
					System.out.println("...调用营销业务应用系统班组巡检员月工作情况接口....///"+jsonData);
					WsService.saveCheckerMdata(jsonData);
				}else{
		        	String message = "调用接口MonthKeyWork失败:"+root.element("errMsg").getText();
		        	systemService.addLog(message, Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR);
		        }
		     }
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
